/*
	This file is part of AVRCppLib.

    AVRCppLib is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AVRCppLib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AVRCppLib.  If not, see <http://www.gnu.org/licenses/>.

    Copyright (C) Maxime Lemonnier 2007-2011
 */

#ifndef I2CHardwareCompatibillity_H_
#define I2CHardwareCompatibillity_H_
#include "../I2CHardware.h"
#include <avr/interrupt.h>
#include "../../../CallbackServers/FastCallbackServer.h"


#define I2C_CREATE(number) \
	new I2CHardware(TWI##number##_vect_number, &TW##number##DR, &TW##number##BR, &TW##number##CR, &TW##number##SR, &TW##number##AR)

#define I2C_CASE(number)\
	case number:		static I2CHardware * i2c##number = NULL;\
						if(isNull(i2c##number))\
							i2c##number = I2C_CREATE(number);\
						instance = i2c##number;\
						break


#define I2C_ISR(number)\
ISR(TWI##number##_vect)\
{\
	FastCallbackServer::getInstance()->dispatch(TWI##number##_vect_number);\
}\


#ifdef I2C0
				I2C_ISR(0);
#endif

namespace Coms
{

namespace I2C
{

I2CHardware* I2CHardware::getInstance(size_t number)
{
	static I2CHardware* instance = NULL;

	switch(number)
	{
#ifdef I2C0
				I2C_CASE(0);
#endif
#ifdef I2C1
				I2C_CASE(1);
#endif

	default:	instance = NULL;
	}

	return instance;
}

}

}


#endif /* I2CHardwareCompatibillity_H_ */
